import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import Qt5Compat.GraphicalEffects
import Fk

Item {
    id: achievementSystem
    anchors.fill: parent

    FontLoader {
        id: steamFont
        source: "../fonts/simhei.ttf"
    }

    FontLoader {
        id: minecraftFont
        source: "../fonts/Minecraft-UTF-8.ttf"
    }

    // 成就类型枚举
    property int steamAchievement: 0
    property int minecraftAchievement: 1

    // 成就队列
    property var achievementQueue: []
    property bool isProcessing: false

    // 成就信息
    property int type: 0
    property int achievementWidth: 250
    property int achievementHeight: 50
    property string title: "Hellow World!"
    property string context: "Welcome to my world!"
    property string imgSrc: "general:zhangfei"

    // 添加成就到队列
    function addAchievement(type, width, height, title, context, imgSrc) {
        // 文本长度限制
        var limitedTitle = limitText(title, 25);
        var limitedContext = limitText(context, 30);
        
        // 处理图片路径
        var processedImgSrc = processImageSource(imgSrc);
        var noclip = imgSrc.indexOf('path:') === 0
        
        // 添加到队列
        achievementQueue.push({
            type: type,
            width: width,
            height: height,
            title: limitedTitle,
            context: limitedContext,
            imgSrc: processedImgSrc,
            noclip: noclip
        });
        
        // 如果没有正在处理的任务，开始处理
        if (!isProcessing) {
            processNextAchievement();
        }
    }

    // 处理图片路径
    function processImageSource(src) {
        if (src.indexOf('path:') === 0) {
            return src.split("path:")[1];
        } else if (src.indexOf('general:') === 0) {
            return SkinBank.getGeneralPicture(src.split("general:")[1]) || (SkinBank.GENERALCARD_DIR + 'card-back');
        }
        return SkinBank.getGeneralPicture("general:zhangfei") || (SkinBank.GENERALCARD_DIR + 'card-back');
    }

    // 文本长度限制
    function limitText(text, maxLength) {
        var count = 0;
        var result = '';
        
        for (var i = 0; i < text.length; i++) {
            // 中文字符算2个长度
            if (text[i].match(/[\u4E00-\u9FA5]/)) {
                count += 2;
            } else {
                count += 1;
            }
            
            if (count <= maxLength) {
                result += text[i];
            } else {
                result += '...';
                break;
            }
        }
        
        return result;
    }

    // 处理下一个成就
    function processNextAchievement() {
        if (achievementQueue.length === 0) {
            isProcessing = false;
            return;
        }
        
        isProcessing = true;
        var achievement = achievementQueue.shift();
        
        if (achievement.type === steamAchievement) {
            createSteamAchievement(achievement);
        } else {
            createMinecraftAchievement(achievement);
        }
    }

    // 创建Steam成就
    function createSteamAchievement(achievement) {
        var component = Qt.createComponent("SteamAchievement.qml");
        if (component.status === Component.Ready) {
            var steamAchievement = component.createObject(achievementSystem, {
                width: achievement.width,
                height: achievement.height,
                title: achievement.title,
                context: achievement.context,
                imgSrc: achievement.imgSrc,
                noclip: achievement.noclip
            });
            
            steamAchievement.show();
            steamAchievement.finished.connect(function() {
                steamAchievement.destroy();
                processNextAchievement();
            });
        } else {
            console.error("Error loading Steam achievement component:", component.errorString());
            processNextAchievement();
        }
    }

    // 创建Minecraft成就
    function createMinecraftAchievement(achievement) {
        var component = Qt.createComponent("MinecraftAchievement.qml");
        if (component.status === Component.Ready) {
            var mcAchievement = component.createObject(achievementSystem, {
                width: achievement.width,
                height: achievement.height,
                title: achievement.title,
                context: achievement.context,
                imgSrc: achievement.imgSrc,
                noclip: achievement.noclip
            });
            
            mcAchievement.show();
            mcAchievement.finished.connect(function() {
                mcAchievement.destroy();
                processNextAchievement();
            });
        } else {
            console.error("Error loading Minecraft achievement component:", component.errorString());
            processNextAchievement();
        }
    }

    // 测试按钮 - 用于调试
    
    /* Row {
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 10
        padding: 10
        
        Button {
            text: "添加Steam成就"
            onClicked: {
                addAchievement(steamAchievement, 250, 50, 
                    "获得成就!", "这是一个Steam成就示例", 
                    "path:../image/anim/Hellow_World.png");
            }
        }
        
        Button {
            text: "添加MC成就"
            onClicked: {
                addAchievement(minecraftAchievement, 250, 50, 
                    "获得成就!", "这是一个Minecraft成就示例", 
                    "path:../image/anim/mirror.png");
            }
        }

        Button {
            text: "添加角色图成就"
            onClicked: {
                addAchievement(minecraftAchievement, 250, 50, 
                    "获得成就!", "这是一个Minecraft成就示例", 
                    "general:sazh__zhonghui");
            }
        }
    } */

    function loadData(data) {
        if (data.type == "mc") {
            type = minecraftAchievement
        }
        if (data.width) achievementWidth = data.width;
        if (data.height) achievementHeight = data.height;
        if (data.title) title = data.title;
        if (data.context) context = data.context;
        if (data.imgSrc) imgSrc = data.imgSrc;

        addAchievement(type, achievementWidth, achievementHeight, title, context, imgSrc)
    }
    
}